package com.ruoyi.agriculture.zs.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.agriculture.zs.domain.ZsQuestionLike;
import com.ruoyi.agriculture.zs.service.IZsQuestionLikeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 问答点赞Controller
 *
 * @author zs
 * @date 2025-07-27
 */
@RestController
@RequestMapping("/zs/questionLike")
public class ZsQuestionLikeController extends BaseController
{
    @Autowired
    private IZsQuestionLikeService zsQuestionLikeService;

    /**
     * 查询问答点赞列表
     */
    @PreAuthorize("@ss.hasPermi('zs:questionLike:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZsQuestionLike zsQuestionLike)
    {
        startPage();
        List<ZsQuestionLike> list = zsQuestionLikeService.selectZsQuestionLikeList(zsQuestionLike);
        return getDataTable(list);
    }

    /**
     * 导出问答点赞列表
     */
    @PreAuthorize("@ss.hasPermi('zs:questionLike:export')")
    @Log(title = "问答点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZsQuestionLike zsQuestionLike)
    {
        List<ZsQuestionLike> list = zsQuestionLikeService.selectZsQuestionLikeList(zsQuestionLike);
        ExcelUtil<ZsQuestionLike> util = new ExcelUtil<ZsQuestionLike>(ZsQuestionLike.class);
        util.exportExcel(response, list, "问答点赞数据");
    }

    /**
     * 获取问答点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('zs:questionLike:query')")
    @GetMapping(value = "/{zsLikeId}")
    public AjaxResult getInfo(@PathVariable("zsLikeId") Long zsLikeId)
    {
        return success(zsQuestionLikeService.selectZsQuestionLikeByZsLikeId(zsLikeId));
    }

    /**
     * 新增问答点赞
     */
    @PreAuthorize("@ss.hasPermi('zs:questionLike:add')")
    @Log(title = "问答点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZsQuestionLike zsQuestionLike)
    {


        return toAjax(zsQuestionLikeService.insertZsQuestionLike(zsQuestionLike));
    }

    /**
     * 修改问答点赞
     */
    @PreAuthorize("@ss.hasPermi('zs:questionLike:edit')")
    @Log(title = "问答点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZsQuestionLike zsQuestionLike)
    {
        return toAjax(zsQuestionLikeService.updateZsQuestionLike(zsQuestionLike));
    }

    /**
     * 删除问答点赞
     */
    @PreAuthorize("@ss.hasPermi('zs:questionLike:remove')")
    @Log(title = "问答点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zsLikeIds}")
    public AjaxResult remove(@PathVariable Long[] zsLikeIds)
    {
        return toAjax(zsQuestionLikeService.deleteZsQuestionLikeByZsLikeIds(zsLikeIds));
    }

    /**
     * 前台添加点赞（无需权限）
     */
    @PostMapping("/addLike")
    public AjaxResult addLike(@RequestBody ZsQuestionLike zsQuestionLike)
    {
        // 检查是否已点赞
        ZsQuestionLike query = new ZsQuestionLike();
        query.setZsQuestionId(zsQuestionLike.getZsQuestionId());
        query.setUserId(zsQuestionLike.getUserId());
        List<ZsQuestionLike> existingLikes = zsQuestionLikeService.selectZsQuestionLikeList(query);

        if (!existingLikes.isEmpty()) {
            return error("您已经点赞过了");
        }

        return toAjax(zsQuestionLikeService.insertZsQuestionLike(zsQuestionLike));
    }

    /**
     * 检查是否已点赞
     */
    @GetMapping("/check")
    public AjaxResult checkLike(@RequestParam("zsQuestionId") Long zsQuestionId, @RequestParam("userId") Long userId)
    {
        ZsQuestionLike query = new ZsQuestionLike();
        query.setZsQuestionId(zsQuestionId);
        query.setUserId(userId);
        List<ZsQuestionLike> likes = zsQuestionLikeService.selectZsQuestionLikeList(query);
        return success(likes != null && !likes.isEmpty());
    }
}
